package cn.iocoder.yudao.module.bpm.service.amis;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.bpm.api.amis.dto.AmisFormConfigDto;
import cn.iocoder.yudao.module.bpm.api.amis.dto.AmisListColumConfigDto;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
import cn.iocoder.yudao.module.bpm.framework.amis.IAmisFormContainerHandler;
import cn.iocoder.yudao.module.bpm.service.definition.BpmFormService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Log4j2
public class AmisFormConfigAnalysisImpl implements AmisFormConfigAnalysis{

    private final static Map<String, IAmisFormContainerHandler> strategyMap = new HashMap<>();

    private final static String AMIS_CONFIG_BODY_KEY = "body";
    private final static String AMIS_CONFIG_TYPE_KEY = "type";


    @Resource
    private BpmFormService formService;

    public AmisFormConfigAnalysisImpl(List<IAmisFormContainerHandler> strategyList) {
        strategyList.forEach(strategy -> {
            IAmisFormContainerHandler oldStrategy = strategyMap.put(strategy.getStrategy(), strategy);
            Assert.isNull(oldStrategy, "策略(%s) 重复", strategy.getStrategy());
        });

    }
    @Override
    public List<AmisListColumConfigDto> analysisColumnList(String formKey, String type) {

        JSONObject amisJson = getAmisJson(formKey);

        JSONObject amisJsonByType = findAmisJsonByType(amisJson, type);

        List<AmisListColumConfigDto> amisListColumConfigDtoList = strategyMap.get(type).handleGetColumns(amisJsonByType);

        return amisListColumConfigDtoList ;
    }



    @Override
    public AmisFormConfigDto analysisColumnForm(String formKey, String type) {
        return null;
    }


    private JSONObject getAmisJson(String formKey) {
        BpmFormDO formByFormKey = formService.getFormByFormKey(formKey);

        String jsonStr = formByFormKey.getJsonStr();
        JSONObject amisJson = JSONObject.parseObject(jsonStr);

        return amisJson;
    }


    private JSONObject findAmisJsonByType(JSONObject amisJson,String type) {
        JSONArray amisBody = amisJson.getJSONArray(AMIS_CONFIG_BODY_KEY);
        AtomicReference<JSONObject> res = new AtomicReference<>();
        amisBody.stream()
                .forEach(item -> {
                    if (item instanceof JSONObject) {
                        JSONObject jsonObject = (JSONObject) item;
                        String itemType = jsonObject.getString(AMIS_CONFIG_TYPE_KEY);
                        if (itemType.equals(type)) {
                            // 找到符合条件的对象，可以进行操作
                            log.info("找到符合条件的对象：" + jsonObject);
                            res.set(jsonObject);
                        }else {
                            findAmisJsonByType(jsonObject,type);
                        }
                    }
                });

        return res.get();
    }


}
